package com.yyx.app1.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.yyx.app1.CommonControls.ImageLoadingDialog;
import com.yyx.app1.Data.BaseDataManager;
import com.yyx.app1.Data.DataInit;
import com.yyx.app1.Data.testDate;
import com.yyx.app1.MyApp;
import com.yyx.app1.R;
import com.yyx.app1.tool.ImageTool;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;


/**
 * Created by yuexing on 2015/10/20.
 */
@EActivity(R.layout.activity_imageshower)
public class ImageShower_Activity extends Activity {
    private ImageLoadingDialog dialog;

    @ViewById
    ImageView imageshower_image;

    @AfterViews
    public void init()
    {
        try
        {
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            BaseDataManager baseDataManager = ((MyApp) getApplication()).getBaseDataManager();
            ImageLoader imageLoader = baseDataManager.getM_imageLoader();
            Bundle bundle = getIntent().getExtras();
            String tagType = bundle.getString("type");
            if (tagType.equals("URL"))
            {
                imageLoader.get(bundle.getString("value"), imageLoader.getImageListener(imageshower_image, testDate.loadImageRes,testDate.defaultMedicationImage), size.x,size.y);
            }
            else //(tagType.equals("URI"))
            {
                Bitmap bitmap = baseDataManager.getUri((Uri) bundle.getParcelable("value"), size.x, size.y,false);
                if(bitmap.getHeight()>size.y || bitmap.getRowBytes()/4>size.x)
                    imageshower_image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                imageshower_image.setImageBitmap(bitmap);
            }
            dialog = new ImageLoadingDialog(this);
            dialog.show();
            // 两秒后关闭后dialog
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                }
            }, 1000);
        }
        catch (Exception e)
        {
            Log.e("异常1510201621",e.toString());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        finish();
        return true;
    }

    @Override
    protected void onDestroy() {
        imageshower_image.setImageDrawable(null);
        System.gc();
        super.onDestroy();
    }

    public static void setTagOnClickListener(ImageView imageView, final Context context, final Bundle tag)
    {
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ImageShower_Activity_.class);
                intent.putExtras((Bundle)v.getTag());
                context.startActivity(intent);
            }
        });
        imageView.setTag(tag);
    }
}
